package com.smasher.core.io;

import android.os.Environment;
import android.os.StatFs;
import android.util.Log;

import com.smasher.core.R;
import com.smasher.core.other.ApplicationContext;

import java.io.File;
import java.text.DecimalFormat;

public class DiskUtil {

    public static final String TAG = "Disk";

    public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.00");
    public static final long KB = 1024;
    public static final long MB = KB * KB;
    public static final long GB = MB * KB;

    /*获取文件大小单位为B的double值*/
    public static final int SIZE_TYPE_B = 1;
    /*获取文件大小单位为KB的double值*/
    public static final int SIZE_TYPE_KB = 2;
    /* 获取文件大小单位为MB的double值*/
    public static final int SIZE_TYPE_MB = 3;
    /* 获取文件大小单位为GB的double值*/
    public static final int SIZE_TYPE_GB = 4;

    public static final long limit = 30L;

    public static boolean checkSDCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    public static String checkDisk() {
        long internalAvailable = getAvailableSize(ApplicationContext.getInstance().getFilesDir().getAbsolutePath());
        if (internalAvailable < limit) {
            return ApplicationContext.getInstance().getString(R.string.no_sd_space);
        }

        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String path = ApplicationContext.getInstance().getFilesDir().getPath();
            long sdcardAvailable = getAvailableSize(path);
            if (sdcardAvailable < 30) {
                return ApplicationContext.getInstance().getString(R.string.no_sd_space);
            }
        } else {
            return ApplicationContext.getInstance().getString(R.string.sd_error);
        }
        return null;
    }

    public static long getAvailableSize(String path) {
        try {
            StatFs stat = new StatFs(path);
            long blockSize = stat.getBlockSizeLong();
            long availableBlocks = stat.getAvailableBlocksLong();
            //MB
            return (availableBlocks * blockSize) / 1024 / 1024;
        } catch (Exception e) {
            Log.e(TAG, "getAvailableSize: ", e);
        }
        return 0;
    }

    /**
     * 获取文件指定文件的指定单位的大小
     *
     * @param filePath 文件路径
     * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
     * @return double值的大小
     */
    public static double getFileOrFilesSize(String filePath, int sizeType) {
        File file = new File(filePath);
        long blockSize = 0;
        try {
            if (file.isDirectory()) {
                blockSize = getFileSizes(file);
            } else {
                blockSize = getFileSize(file);
            }
        } catch (Exception e) {
            Log.e(TAG, "getFileOrFilesSize: ", e);
        }
        return formatFileSize(blockSize, sizeType);
    }

    /**
     * 获取指定文件夹
     *
     * @param f f
     * @return long
     */
    public static long getFileSizes(File f) {
        long size = 0;
        File[] files = f.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file != null) {
                    if (file.isDirectory()) {
                        size = size + getFileSizes(file);
                    } else {
                        size = size + getFileSize(file);
                    }
                }
            }
        }
        return size;
    }

    /**
     * 获取指定文件大小
     *
     * @return long
     */
    public static long getFileSize(File file) {
        if (file == null || !file.exists()) {
            return 0;
        }
        return file.length();
    }

    /**
     * 转换文件大小
     *
     * @param fileSize fileSize
     * @return String
     */
    public static String formatFileSize(long fileSize) {
        if (fileSize == 0) {
            return "0B";
        }

        double size = fileSize;
        String unit;

        if (size < KB) {
            unit = "B";
        } else if (size < MB) {
            size /= KB;
            unit = "KB";
        } else if (size < GB) {
            size /= MB;
            unit = "MB";
        } else {
            size /= GB;
            unit = "GB";
        }

        return DECIMAL_FORMAT.format(size) + unit;
    }

    /**
     * 转换文件大小,指定转换的类型
     *
     * @param fileSize fileSize
     * @param sizeType sizeType
     * @return double
     */
    public static double formatFileSize(long fileSize, int sizeType) {
        return switch (sizeType) {
            case SIZE_TYPE_B -> fileSize;
            case SIZE_TYPE_KB -> (double) fileSize / KB;
            case SIZE_TYPE_MB -> (double) fileSize / MB;
            case SIZE_TYPE_GB -> (double) fileSize / GB;
            default -> throw new IllegalArgumentException("未知的单位类型: " + sizeType);
        };
    }
}
